package com.dengjiawen.project01.bag.impl;

import com.dengjiawen.project01.bag.MyObjectList;

import java.util.Random;

/**
 * This class impl the MyObjectList interface.
 *
 * @author Jarvan
 * @version 1.0
 * @create 2020/9/22 20:58
 */
public class MyObjectListImpl implements MyObjectList {

    /**
     * This bag should hold up to 100 items.
     * And Ww should init it in the constructor if we impl this interface.
     */
    Object[] objects = new Object[100];

    /**
     * No parameter structure
     */
    public MyObjectListImpl() {
    }

    /**
     * Init the object by param constructorObjects
     *
     * @param constructorObjects constructorObjects
     */
    public MyObjectListImpl(Object[] constructorObjects) {
        if (constructorObjects.length <= objects.length) {
            for (int i = 0; i < constructorObjects.length; i++) {
                objects[i] = constructorObjects[i];
            }
        } else {
            System.out.println("the length of constructorObjects is=" + constructorObjects.length + " more than object length=" + objects.length);
            throw new IndexOutOfBoundsException();
        }
    }

    /**
     * Add an item to the end of the list of this bag
     *
     * @param insertObject insert object.
     * @return insert Object if array has any empty item.
     */
    @Override
    public Object insert(Object insertObject) {
        if (size() <= objects.length) {
            objects[size()] = insertObject;
        } else {
            System.err.println("this array of object is full ,now this size()=" + size());
            throw new IndexOutOfBoundsException();
        }
        return insertObject;
    }

    /**
     * Remove the item at the end of this bag if the array has any items.
     *
     * @return the Object that been removed.
     */
    @Override
    public Object removeLast() {
        //if has any items.you can remove it.
        Object returnObject = null;
        if (size() > 0) {
            returnObject = objects[size() - 1];
            objects[size() - 1] = null;
        } else {
            System.err.println("there have not any items.");
            throw new IndexOutOfBoundsException();
        }
        return returnObject;
    }

    /**
     * Remove an item at a random index from this bag
     *
     * @return the Object that been removed.
     */
    @Override
    public Object removeRandom() {

        int oldSize = size();
        int random = new Random().nextInt(oldSize);
        return removeByIndex(random);
    }

    /**
     * Remove the item by the index of the array.
     *
     * @param indexOfObjectArrays index of tne array
     * @return the Object that been removed.
     */
    @Override
    public Object removeByIndex(int indexOfObjectArrays) {
        Object returnObject = null;
        if (size() > 0) {
            //we should use old size to prevent the change of size . and can save resources.
            int oldSize = size();
            int random = new Random().nextInt(oldSize);
            returnObject = objects[random];
            objects[random] = null;
            //we must revalue this objects because this is list class.
            for (; (random + 1) < oldSize; random++) {
                objects[random] = objects[random + 1];

            }
            //remove the last item.
            objects[oldSize - 1] = null;
        } else {
            System.err.println("there have not any items.");

        }
        return returnObject;
    }

    /**
     * Get a get(a reference to an item)
     *
     * @param object object.
     * @return get(a reference to an item), if haven not this object ,it will return -1.
     */
    @Override
    public int getIndex(Object object) {

        for (int i = 0; i < objects.length; i++) {
            if (objects[i].equals(object)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Get a Object from the array object by index.
     *
     * @param index index.
     * @return a Object from the array object by index.
     */
    @Override
    public Object getObject(int index) {
        return objects[index];
    }

    /**
     * Check how many items are there in this bag
     *
     * @return number of the items.sizeNotNull
     */
    @Override
    public int size() {
        int sizeNotNull = 0;
        for (int i = 0; i < objects.length; i++) {
            if (objects[i] != null) {
                sizeNotNull++;
            }

        }
        return sizeNotNull;
    }

    /**
     * Check to see if this bag is empty
     *
     * @return true if the array of object is empty,and false if No
     */
    @Override
    public boolean isEmpty() {
        if (size() == 0) {
            return true;
        }
        return false;
    }

    /**
     * Empty this bag,if the array of object has any items ,clean up the array.
     */
    @Override
    public void makeEmpty() {
        for (int i = 0; i < size(); i++) {
            objects[i] = null;
        }
    }

    public Object[] getList() {
        return objects;
    }
}
